import socket
host = 'www.google.com'
addr = socket.gethostbyname(host)
print(f"The ip address for {host} - ", addr)The ip address for www.google.com - 142.250.192.68
2023-05-27
securedHTTP/1.1 200 OK
Date: Fri, 26 May 2023 08:40:09 GMT
Server: Apache/2.4.18 (Ubuntu)
Last-Modified: Sat, 13 May 2017 11:22:22 GMT
ETag: "a7-54f6609245537"
Accept-Ranges: bytes
Content-Length: 167
Cache-Control: max-age=0, no-cache, no-store, must-revalidate
Pragma: no-cache
Expires: Wed, 11 Jan 1984 05:00:00 GMT
Connection: close
Content-Type: text/plain
But soft what light through yonder window breaks
It is the east and Juliet is the sun
Arise fair sun and kill the envious moon
Who is already sick and pale with grief
%%writefile serverprog.py
import socket
#SERVER NAME AND ADDRESS
host = 'localhost'
port = 5000
#CREATE SERVER SIDE SOCKET USING TCP
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#BIND SOCKET WITH SERVER AND PORT
s.bind((host,port))
#ALLOW MAX ONE CONNECTION TO THE SOCKET
s.listen(1)
# WAIT TILL CLIENT ACCEPTS CONNECTION
c,addr = s.accept()
# DISPLAY CLIENT ADDRESS
print("connection established from - ", str(addr))
# SEND ENCODED MESSAGE TO CLIENT
c.send(b"Hello client, How are you?")
msg = 'bye'
c.send(msg.encode())
# DISCONNECT SERVER
c.close()Overwriting serverprog.py
%%writefile clientprog.py
import socket
# TAKE SERVER NAME AND ADDRESS
host = 'localhost'
port = 5000
#CREATE CLIENT SIDE SOCKET USING TCP
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#CONNECT SOCKET WITH SERVER AND PORT
s.connect((host,port))
# RECEIVE MAX 1024 BYTES MESSAGE FROM SERVER AT A TIME
msg = s.recv(1024)
# REPEAT AS LONG AS MESSAGE IS NOT EMPTY
while msg:
print("Received - ", msg.decode())
msg = s.recv(1024)
# DISCONNECT SERVER
s.close()Overwriting clientprog.py
%%writefile udpserver.py
import socket
import time
#SERVER NAME AND ADDRESS
host = 'localhost'
port = 5000
#CREATE SERVER SIDE SOCKET USING TCP
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# SERVER WAITS FOR 5 SECONDS
time.sleep(5)
# SEND ENCODED MESSAGE TO CLIENT
s.sendto(b"Hello client, How are you?", (host,port))
msg = 'bye'
s.sendto(msg.encode(),(host,port))
# DISCONNECT SERVER
s.close()Writing udpserver.py
%%writefile udpclient.py
import socket
# TAKE SERVER NAME AND ADDRESS
host = 'localhost'
port = 5000
#CREATE CLIENT SIDE SOCKET USING TCP
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
#CONNECT SOCKET WITH SERVER AND PORT
s.bind((host,port))
# RECEIVE MAX 1024 BYTES MESSAGE FROM SERVER AT A TIME
msg,addr = s.recvfrom(1024)
try :
# SOCKET BLOCKS AFTER 5 SEC IF SERVER DISCONNECTS
s.settimeout(5)
# REPEAT AS LONG AS MSG IS NOT EMPTY
while msg:
print("Received - ", msg.decode())
msg,addr = s.recvfrom(1024)
except socket.timeout:
print("time is over and hence terminating")
# DISCONNECT SERVER
s.close()Overwriting udpclient.py
%%writefile server2.py
import socket
#SERVER NAME AND ADDRESS
host = 'localhost'
port = 9000
#CREATE SERVER SIDE SOCKET USING TCP
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#BIND SOCKET WITH SERVER AND PORT
s.bind((host,port))
#ALLOW MAX ONE CONNECTION TO THE SOCKET
s.listen(1)
# WAIT TILL CLIENT ACCEPTS CONNECTION
c,addr = s.accept()
# DISPLAY CLIENT ADDRESS
print("connection established from - ", str(addr))
# RUN SERVER CONTINUOUSLY
while True:
data = c.recv(1024)
#IF CLIENT SENDS EMPTY STRING, COME OUT OF WHILE LOOP
if not data:
break
print("From client - " + str(data.decode()))
# ENTER RESPONSE FROM SERVER
data1 = input("Enter the response = ")
# SEND THAT DATA TO CLIENT
c.send(data1.encode())
# DISCONNECT SERVER
c.close()Writing server2.py
%%writefile client2.py
import socket
# TAKE SERVER NAME AND ADDRESS
host = 'localhost'
port = 9000
#CREATE CLIENT SIDE SOCKET USING TCP
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#CONNECT SOCKET WITH SERVER AND PORT
s.connect((host,port))
# ENTER DATA AT CLIENT
str = input("Enter data - ")
# REPEAT AS LONG AS MESSAGE IS NOT exit
while str != 'exit':
# SEND DATA FROM CLIENT TO SERVER
s.send(str.encode())
# RECEIVE RESPONSE DATA FROM SERVER
data = s.recv(1024)
data = data.decode()
print("From server - " + data)
# ENTER DATA
str = input("Enter data - ")
# DISCONNECT SERVER
s.close()Overwriting client2.py
# importing the sockets module
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
target = input('What you want to scan?: ')
# getting the ip address using gethostbyname function
t_IP = socket.gethostbyname(target)
print("Starting scan on host: ", t_IP)
#create function port_scan
def port_scan(port):
try:
s.connect((t_IP, port))
return True
except:
return False
#user input to ask for port number to be scanned
port = int(input("Enter the port number to be scanned: "))
if port_scan(port):
print('Port', port, 'is open')
else:
print("port", port, "is closed")What you want to scan?: www.google.com
Starting scan on host: 142.250.183.164
Enter the port number to be scanned: 80
Port 80 is open
Manish Patel